﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Character_Creator
{
    public class AbilityScore : IRollingMethods
    {
        uint[] scores;
        int[] mods;
        ROLL_METHODS rollMethod;
        Random rand;

        public uint[] Scores
        {
            get { return scores; }
            private set { scores = value; }
        }
        public int[] Modifiers
        {
            get { return mods; }
            private set { mods = value; }
        }
        public ROLL_METHODS RollMethod
        {
            get { return rollMethod; }
            set { rollMethod = value; }
        }

        public AbilityScore()
        {
            scores = new uint[6];
            mods = new int[6];
            rollMethod = ROLL_METHODS.ROLL_STANDARD;
            rand = new Random(DateTime.Now.Millisecond);
        }

        public void RollScores()
        {
            switch (rollMethod)
            {
                case ROLL_METHODS.ROLL_STANDARD:
                {
                    StandardMethod();
                    break;
                }
                case ROLL_METHODS.ROLL_CLASSIC:
                {
                    ClassicMethod();
                    break;
                }
                case ROLL_METHODS.ROLL_HEROIC:
                {
                    HeroicMethod();
                    break;
                }
            }
        }
        public void SetAbilityScores(uint[] scores)
        {
            Scores = scores;
            CalculateModifiers();
        }
        private void CalculateModifiers()
        {
            for (uint mod = 0; mod < 6; ++mod)
            {
                Modifiers[mod] = (int)(scores[mod] / 2) - 5;
            }
        }

        public void ClassicMethod()
        {
            Scores = ClassicMethodRoll();
            CalculateModifiers();
        }
        private uint[] ClassicMethodRoll()
        {
            // Roll three numbers
            // Sum them
            // Set the sum to the current score being rolled
            uint[] scores = new uint[6];
            for (uint score = 0; score < 6; ++score)
            {
                uint sum = 0;
                for (uint roll = 0; roll < 3; ++roll)
                {
                    uint number = (uint)rand.Next(1, 6);
                    sum += number;
                }

                scores[score] = sum;
            }

            return scores;
        }
        public void StandardMethod()
        {
            Scores = StandardMethodRoll();
            CalculateModifiers();
        }
        private uint[] StandardMethodRoll()
        {
            // Roll four numbers
            // Disregard the lowest
            // Sum the remaining three
            // Set the sum to the current score being rolled
            uint[] scores = new uint[6];
            for (uint score = 0; score < 6; ++score)
            {
                uint sum = 0;
                List<uint> rolls = new List<uint>();
                for(uint roll = 0; roll < 4; ++roll)
                {
                    uint number = (uint)rand.Next(1, 6);
                    rolls.Add(number);
                }

                rolls.Sort();
                rolls.Remove(rolls[0]);

                foreach (uint number in rolls)
                {
                    sum += number;
                }

                scores[score] = sum;
            }

            return scores;
        }
        public void HeroicMethod()
        {
            Scores = HeroicMethodRoll();
            CalculateModifiers();
        }
        private uint[] HeroicMethodRoll()
        {
            // Roll two numbers
            // Sum them with the number 6
            // Set the sum to be the current score being rolled
            uint[] scores = new uint[6];
            for (uint score = 0; score < 6; ++score)
            {
                uint sum = 6;
                for (uint roll = 0; roll < 2; ++roll)
                {
                    uint number = (uint)rand.Next(1, 6);
                    sum += number;
                }

                scores[score] = sum;
            }

            return scores;
        }
    }
}
